namespace SimpleWebServices.Configuration
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class HttpHeaderSettingsFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<HttpHeaderSettings>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("headers")
                .Implements<IXmlSerializable>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            HttpHeaderSettings actual = (
                "<headers>" +
                    "<absent>Server</absent>" +
                    "<absent>X-Powered-By</absent>" +
                "</headers>").Deserialize<HttpHeaderSettings>();

            Assert.NotNull(actual);

            Assert.NotNull(actual.Absent);
            Assert.Equal<int>(2, actual.Absent.Count);

            Assert.Equal<string>("Server", actual.Absent[0]);
            Assert.Equal<string>("X-Powered-By", actual.Absent[1]);
        }

        [Fact]
        public void deserializeEmpty()
        {
            HttpHeaderSettings actual = "<headers />".Deserialize<HttpHeaderSettings>();

            Assert.NotNull(actual);

            Assert.NotNull(actual.Absent);
            Assert.Empty(actual.Absent);
        }

        [Fact]
        public void serialize()
        {
            HttpHeaderSettings obj = new HttpHeaderSettings
            {
                Absent =
                {
                    "Server",
                    "X-Powered-By"
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/headers/absent[text()='Server'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/headers/absent[text()='X-Powered-By'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new HttpHeaderSettings());
        }

        [Fact]
        public void prop_Absent()
        {
            Assert.True(new PropertyExpectations(typeof(HttpHeaderSettings).GetProperty("Absent"))
                .TypeIs<Collection<string>>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new HttpHeaderSettings() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new HttpHeaderSettings() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new HttpHeaderSettings() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}